//
//  JMForgetViewController.m
//  VoluntaryStar
//
//  Created by xiaorui on 15/9/1.
//  Copyright (c) 2015年 ios. All rights reserved.
//

#import "JMForgetViewController.h"
#import "JMAppNetworkClient.h"

@interface JMForgetViewController ()<UITextFieldDelegate>

@property (nonatomic, strong) UIImageView *backImageView;

@property (nonatomic, strong) UIImageView *topImageView;
@property (nonatomic, strong) UILabel *topImageLabel;

@property (nonatomic, strong) UIImageView *loginbackImageView;

@property (nonatomic, strong) UITextField *iphoneTextField;

@property (nonatomic, strong) UITextField *emailTextField;

@property (nonatomic, strong) UIButton *loginBtn;

@end

@implementation JMForgetViewController

#pragma mark chu shi hua jie mian kong jian

- (UIImageView *)backImageView
{
    if (!_backImageView) {
        UIImageView *imageView = [[UIImageView alloc]init];
        imageView.image = [UIImage imageNamed:@"login_regist.bundle/back_image"];
        imageView.contentMode = UIViewContentModeScaleAspectFill;
        imageView.backgroundColor = [UIColor clearColor];
        imageView.userInteractionEnabled = YES;
        _backImageView = imageView;
        [self.view addSubview:_backImageView];
    }
    return _backImageView;
}

- (UIImageView *)topImageView{
    if(!_topImageView){
        _topImageView = [[UIImageView alloc]init];
        _topImageView.backgroundColor = [UIColor clearColor];
        _topImageView.image = [UIImage imageNamed:@"login_regist.bundle/topBack_image"];
        [_topImageView addSubview:self.topImageLabel];
        [self.view addSubview:_topImageView];
    }
    return _topImageView;
}
- (UILabel *)topImageLabel
{
    if (!_topImageLabel) {
        UILabel* label = [[UILabel alloc]init];
        label.backgroundColor = [UIColor clearColor];
        label.textColor = [UIColor blackColor];
        label.font = [UIFont systemFontOfSize:14.0f];
        label.text = @"找回密码";
        label.textAlignment = NSTextAlignmentCenter;
        label.textColor = [UIColor whiteColor];
        _topImageLabel = label;
        [self.topImageView addSubview:_topImageLabel];
    }
    return _topImageLabel;
}

-(UIImageView *)loginbackImageView{
    if(!_loginbackImageView){
        _loginbackImageView = [[UIImageView alloc]init];
        _loginbackImageView.backgroundColor = [UIColor clearColor];
        _loginbackImageView.image = [UIImage imageNamed:@"login_regist.bundle/regist_login_back"];
        _loginbackImageView.userInteractionEnabled =YES;
        [self.view addSubview:_loginbackImageView];
    }
    return _loginbackImageView;
}

- (UITextField *)iphoneTextField{
    if(!_iphoneTextField){
        _iphoneTextField = [[UITextField alloc]init];
        _iphoneTextField.placeholder =@"请输入手机号";
        _iphoneTextField.textColor = [UIColor whiteColor];
        //        _userNameTextField.font = [XRTools PXFontConversionIOS:28];
        [_iphoneTextField setValue:[UIColor whiteColor] forKeyPath:@"_placeholderLabel.textColor"];
        _iphoneTextField.delegate =self;
        [self.loginbackImageView addSubview:_iphoneTextField];
        
    }
    return _iphoneTextField;
}

- (UITextField *)emailTextField{
    if(!_emailTextField){
        _emailTextField = [[UITextField alloc]init];
        _emailTextField.backgroundColor = [UIColor clearColor];
        _emailTextField.placeholder = @"请输入邮箱";
        _emailTextField.textColor = [UIColor whiteColor];
        [_emailTextField setValue:[UIColor whiteColor] forKeyPath:@"_placeholderLabel.textColor"];
        _emailTextField.delegate =self;
        //        _pwdTextField.font = [XRTools PXFontConversionIOS:28];
        [self.loginbackImageView addSubview:_emailTextField];
    }
    return _emailTextField;
}


- (UIButton *)loginBtn{
    if(!_loginBtn){
        _loginBtn = [UIButton buttonWithType:UIButtonTypeCustom];;
        [_loginBtn setBackgroundImage:[UIImage imageNamed:@"login_regist.bundle/seleted_backImage@2x"] forState:UIControlStateNormal];
        _loginBtn.backgroundColor = [UIColor clearColor];
        [_loginBtn setTitle:@"确定" forState:UIControlStateNormal];
        _loginBtn.titleLabel.font = [UIFont systemFontOfSize:14];
        [_loginBtn addTarget:self action:@selector(loginButtonClick:) forControlEvents:UIControlEventTouchUpInside];
        [self.loginbackImageView addSubview:_loginBtn];
    }
    return _loginBtn;
}


-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:YES];
    [[UITextField appearance] setTintColor:[UIColor whiteColor]];
    self.navigationController.navigationBarHidden = YES;
    self.navBackBtn.hidden =YES;
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyBoardShow:) name:UIKeyboardDidShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyBoardHidden:) name:UIKeyboardDidHideNotification object:nil];
}

-(void)viewDidLoad{
    [super viewDidLoad];
    self.backImageView.frame = self.view.bounds;
    self.topImageView.frame = CGRectMake(UI_SCREEN_WITH/2.0 - UI_SCREEN_WITH *0.25625/2.0, 26, UI_SCREEN_WITH *0.25625, UI_SCREEN_WITH *0.0875);
    self.topImageLabel.frame = CGRectMake(0, 0, self.topImageView.frameWidth, self.topImageView.frameHeight);
    
    CGFloat login ;
    if(iphone4){
        login = 60;
    }else if (iphone5){
        login = 100;
    }else if(iphone6){
        login = 120;
    }else{
        login = 140;
    }
    self.loginbackImageView.frame = CGRectMake(0, login, UI_SCREEN_WITH, UI_SCREEN_WITH *0.671875);
    self.iphoneTextField.frame = CGRectMake(UI_SCREEN_WITH *0.09375, UI_SCREEN_WITH *0.171875, UI_SCREEN_WITH - UI_SCREEN_WITH *0.09375 *2, UI_SCREEN_WITH *0.125);

    
    self.emailTextField.frame = CGRectMake(self.iphoneTextField.frameLeft, self.iphoneTextField.frameBottom, self.iphoneTextField.frameWidth, self.iphoneTextField.frameHeight);

    self.loginBtn.frame = CGRectMake(UI_SCREEN_WITH/2.0 - UI_SCREEN_WITH *0.234375/2.0, UI_SCREEN_WITH *0.49, UI_SCREEN_WITH *0.234375, UI_SCREEN_WITH *0.07031);

    
    [self.iphoneTextField becomeFirstResponder];
    [self.emailTextField becomeFirstResponder];
    
}
#pragma mark  登陆按钮点击
-(void)loginButtonClick:(id)sender{
    if(![JMTools isiPhoneNumber:self.iphoneTextField.text]){
        [JMTools showAlertView:self.view message:@"你输入的不是手机号"];
        return;
    }
    if(![JMTools isValidateEmail:self.emailTextField.text]){
        [JMTools showAlertView:self.view message:@"你输入的邮箱有误"];
        return;
    }
    NSMutableDictionary *dict =[[NSMutableDictionary alloc]initWithCapacity:0];
    [dict setObject:self.iphoneTextField.text forKey:@"mobile"];
    [dict setObject:self.emailTextField.text forKey:@"email"];

    [JMAppNetworkClient getBackPwdWithInfo:dict complete:^(id data, NSError *error) {
        
        if([data[@"status"]integerValue] == 1){
            [JMTools showAlertView:self.view message:data[@"message"]];
            [self.navigationController popViewControllerAnimated:YES];
        }else{
             [JMTools showAlertView:self.view message:data[@"message"]];
        }
    }];
}

#pragma mark  jie mian shang xiang guan kong jian de dai li fang fa


#pragma mark -- jian pan huo dong
-(void)keyBoardShow:(NSNotification *)notion{
    NSDictionary *info = [notion userInfo];
    NSNumber *timeValue = info[UIKeyboardAnimationDurationUserInfoKey];
    [UIView animateWithDuration:[timeValue floatValue] animations:^{
        if(iphone4){
             self.loginbackImageView.frame = CGRectMake(self.loginbackImageView.frameLeft,self.loginbackImageView.frameTop - 40,self.loginbackImageView.frameWidth,self.loginbackImageView.frameHeight);
        }
    } completion:^(BOOL finished) {
        
    }];
}

-(void)keyBoardHidden:(NSNotification *)notion{
    NSDictionary *info = [notion userInfo];
    NSNumber *timeValue = info[UIKeyboardAnimationDurationUserInfoKey];
    [UIView animateWithDuration:[timeValue floatValue] animations:^{
        if(iphone4){
            self.loginbackImageView.frame = CGRectMake(self.loginbackImageView.frameLeft,self.loginbackImageView.frameTop + 40,self.loginbackImageView.frameWidth,self.loginbackImageView.frameHeight);
        }
    } completion:^(BOOL finished) {
        
    }];
}

#pragma mark  重写返回方法
-(void)backBtnClick:(id)sender{
    [self.iphoneTextField resignFirstResponder ];
    [self.emailTextField resignFirstResponder];
//    [self dismissViewControllerAnimated:YES completion:^{
//        
//    }];
    [self.navigationController popViewControllerAnimated:YES];
}
-(void)didReceiveMemoryWarning{
    [super didReceiveMemoryWarning];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
